package com.example.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.service.UserService;
import com.example.utils.JwtHelper;
import com.example.utils.MD5Util;
import com.example.utils.Result;
import com.example.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wqe
 * @description 针对表【news_user】的数据库操作Service实现
 * @createDate 2024-03-14 11:32:59
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(User user) {
        // 根据用户名查询对应的用户信息
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User user1 = userMapper.selectOne(lambdaQueryWrapper);

        // 查询结果为空 账号错误 返回501
        if (user1 == null) return Result.build(null, ResultCodeEnum.USERNAME_ERROR);

        // 对比密码  密码不为空 且 数据库中的密码(加密后)与用户输入的密码加密后 相同 成功!
        if (StringUtils.isEmpty(user.getUserPwd()) || !user1.getUserPwd().equals(MD5Util.encrypt(user.getUserPwd())))
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);

        String token = jwtHelper.createToken(Long.valueOf(user1.getUid()));
        Map data = new HashMap();
        data.put("token", token);
        return Result.ok(data);
    }

    @Override
    public Result getUserInfo(String token) {
        // 1. 验证token是否过期
        boolean flag = jwtHelper.isExpiration(token);

        // 失效未登录
        if (flag) return Result.build(null, ResultCodeEnum.NOTLOGIN);

        Long userId = jwtHelper.getUserId(token);
        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        Map data = new HashMap();
        data.put("loginUser", user);
        return Result.ok(data);
    }

    /**
     * 使用count进行查询
     * count = 0 -> 可用
     * count > 0 -> 不可用
     *
     * @param username
     * @return
     */
    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        // User user = userMapper.selectOne(lambdaQueryWrapper);
        //if (user == null) return Result.ok(null);

        long count = userMapper.selectCount(lambdaQueryWrapper);
        if (count == 0) return Result.ok(null);

        return Result.build(null, ResultCodeEnum.USERNAME_USED);
    }

    @Override
    public Result register(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        long count = userMapper.selectCount(lambdaQueryWrapper);
        if (count != 0) return Result.build(null, ResultCodeEnum.USERNAME_USED);

        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        userMapper.insert(user);
        return Result.ok(null);
    }
}




